CREATE TRIGGER TGPN_01 ON PHIEUNHAP
FOR INSERT,UPDATE
AS
BEGIN
	DECLARE @TONG MONEY
	SET @TONG = (SELECT TONGTIEN FROM inserted)
	IF(@TONG < 0)
	BEGIN
		RAISERROR('TONG TIEN KHONG DUOC PHEP NHO HON 0',16,1)
		ROLLBACK TRAN
		RETURN
	END
END

CREATE TRIGGER TGPN_02 ON PHIEUNHAP
INSTEAD OF DELETE
AS
BEGIN
	DECLARE @MAPN VARCHAR(50)
	SET @MAPN = (SELECT MAPHIEUNHAP FROM deleted)
	DELETE FROM ChiTietPhieuNhap WHERE MaPhieuNhap = @MAPN
	DELETE FROM PhieuNhap WHERE MaPhieuNhap=@MAPN
	RAISERROR('XOA CHI TIET NHAP THANH CONG',16,1)
END